const { EvaluateModel, UserModel, OrderModel } = require('../../models/index')
const { Op } = require('sequelize')

// 获取评价列表信息
const getEvaluateList = async (req, res) => {
  try {
    // 从分页中间件中获取分页信息
    const { offset, pageSize } = req.pagination;

    const evaluateList = await EvaluateModel.findAndCountAll({
      include: [
        {
          model: UserModel,
          as: 'user',
          attributes: ['username', 'phone', 'avatar']
        },
        {
          model: OrderModel,
          as: 'order',
          attributes: ['place_name', 'product_type', 'quantity', 'price', 'total_amount']
        }
      ],
      order: [
        ['createdAt', 'DESC']
      ],
      offset: offset,
      limit: parseInt(pageSize, 10)
    })

    res.status(200).json({
      title: '评价列表信息',
      data: evaluateList
    })

  } catch (error) {
    res.status(500).json({ error: error.message })
  }
}

// 评价列表搜索功能
const searchEvaluate = async (req, res) => {
  try {
    // 获取用户输入的搜索词
    const searchText = req.query.data

    // 如果搜索词为空返回空数组
    if (!searchText || searchText.trim() === '') {
      return res.status(200).json([])
    }

    // 搜索匹配内容
    const evaluateItem = await EvaluateModel.findAll({
      include: [
        {
          model: UserModel,
          as: 'user',
          attributes: ['username', 'phone', 'avatar']
        },
        {
          model: OrderModel,
          as: 'order',
          attributes: ['place_name', 'product_type', 'quantity', 'price', 'total_amount']
        }
      ],
      where: {
        [Op.or]: [
          { rating: { [Op.like]: `%${searchText}%` } },
          { comment: { [Op.like]: `%${searchText}%` } },
          { createdAt: { [Op.like]: `%${searchText}%` } },
          { '$user.username$': { [Op.like]: `%${searchText}%` } },
          { '$user.phone$': { [Op.like]: `%${searchText}%` } },
          { '$order.place_name$': { [Op.like]: `%${searchText}%` } },
          { '$order.product_type$': { [Op.like]: `%${searchText}%` } },
          { '$order.price$': { [Op.like]: `%${searchText}%` } },
          { '$order.quantity$': { [Op.like]: `%${searchText}%` } },
          { '$order.total_amount$': { [Op.like]: `%${searchText}%` } },
        ]
      }
    })

    // 返回查询结果
    return res.status(200).json({
      title: '搜索成功',
      data: evaluateItem
    })
  } catch (error) {
    res.status(500).json({ error: error.message })
  }
}

// 删除评论
const deleteEvaluate = async (req, res) => {
  try {
    // 获取评论ID
    const evaluateID = req.params.id

    const result = await EvaluateModel.destroy({
      where: {
        evaluate_id: evaluateID
      }
    })

    // 检查是否有记录被删除
    if (result === 0) {
      return res.status(404).json({ error: '没有找到需要删除的评论记录！' })
    }

    // 删除成功后返回响应
    res.status(200).json({
      title: '删除成功',
      message: '评论记录已被成功删除!',
      data: result
    })
  } catch (error) {
    res.status(500).json({ error: error.message })
  }
}

module.exports = {
  getEvaluateList,
  searchEvaluate,
  deleteEvaluate
}